<?php

namespace app\admin\model;

use think\Model;


class Car extends Model
{

    

    

    // 表名
    protected $name = 'car';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'gearbox_text',
        'Registration_time_text',
        'Compulsory_insurance_due_time_text',
        'Inspection_due_time_text',
        'Create_Time_text',
        'color_text',
        'vehicle_type_text',
        'country_text',
        'status_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }
    public function user()
    {
        return $this->belongsTo('user', 'user_id');
    }
    
    public function getGearboxList()
    {
        return ['manual' => __('Gearbox manual'), 'automatic' => __('Gearbox automatic'), 'Hand_in_one' => __('Gearbox hand_in_one'), 'Dual_clutch' => __('Gearbox dual_clutch'), 'CVT_continuously_variable' => __('Gearbox cvt_continuously_variable'), 'Seven_speed_dual_clutch' => __('Gearbox seven_speed_dual_clutch')];
    }

    public function getColorList()
    {
        return ['black' => __('Color black'), 'white' => __('Color white'), 'red' => __('Color red'), 'blue' => __('Color blue'), 'grey' => __('Color grey'), 'green' => __('Color green'), 'yellow' => __('Color yellow'), 'silver' => __('Color silver'), 'orange' => __('Color orange'), 'golden' => __('Color golden'), 'purple' => __('Color purple'), 'Champagne_gold' => __('Color champagne_gold'), 'brown' => __('Color brown'), 'other' => __('Color other')];
    }

    public function getVehicleTypeList()
    {
        return ['car' => __('Vehicle_type car'), 'SUV' => __('Vehicle_type suv'), 'MPV' => __('Vehicle_type mpv'), 'Dual_clutch' => __('Vehicle_type dual_clutch'), 'CVT_continuously_variable' => __('Vehicle_type cvt_continuously_variable'), 'Seven_speed_dual_clutch' => __('Vehicle_type seven_speed_dual_clutch')];
    }

    public function getCountryList()
    {
        return ['china' => __('Country china'), 'german' => __('Country german'), 'european' => __('Country european'), 'japanese' => __('Country japanese'), 'france' => __('Country france'), 'america' => __('Country america'), 'korean' => __('Country korean')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3')];
    }


    public function getGearboxTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['gearbox']) ? $data['gearbox'] : '');
        $list = $this->getGearboxList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i", $value) : $value;
    }


    public function getRegistrationTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['Registration_time']) ? $data['Registration_time'] : '');
        return is_numeric($value) ? date("Y-m", $value) : $value;
    }


    public function getCompulsoryInsuranceDueTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['Compulsory_insurance_due_time']) ? $data['Compulsory_insurance_due_time'] : '');
        return is_numeric($value) ? date("Y-m", $value) : $value;
    }


    public function getInspectionDueTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['Inspection_due_time']) ? $data['Inspection_due_time'] : '');
        return is_numeric($value) ? date("Y-m", $value) : $value;
    }


    public function getColorTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['color']) ? $data['color'] : '');
        $list = $this->getColorList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getVehicleTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['vehicle_type']) ? $data['vehicle_type'] : '');
        $list = $this->getVehicleTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCountryTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['country']) ? $data['country'] : '');
        $list = $this->getCountryList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setRegistrationTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setCompulsoryInsuranceDueTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setInspectionDueTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


}
